package com.kn.keywordmatch.config;


import com.kn.keywordmatch.common.RabbitMqCon;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
public class RabbitMqConfig {

    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange(RabbitMqCon.TOPIC_EXCHANGE);
    }
    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange(RabbitMqCon.DIRECT_EXCHANGE);
    }

    @Bean
    public Queue hanDanQueue() {
        return new Queue(RabbitMqCon.HAN_DAN_QUEUE, true); // 第二个参数是durable，即是否持久化
    }
    @Bean
    public Queue xiaoJiQueue() {
        return new Queue(RabbitMqCon.XIAO_JI_QUEUE, true); // 第二个参数是durable，即是否持久化
    }
    @Bean
    public Queue directionQueue() {
        return new Queue(RabbitMqCon.DIRECTION_QUEUE, true); // 第二个参数是durable，即是否持久化
    }
    @Bean
    public Queue keywordQueue() {
        return new Queue(RabbitMqCon.KEYWORD_QUEUE, true); // 第二个参数是durable，即是否持久化
    }
    @Bean
    public Queue KeywordExpressionQueue() {
        return new Queue(RabbitMqCon.KEYWORD_EXPRESSION_QUEUE, true); // 第二个参数是durable，即是否持久化
    }
    @Bean
    public Queue PushQueue() {
        return new Queue(RabbitMqCon.PUSH_MATCHED_DATA_QUEUE, true); // 第二个参数是durable，即是否持久化
    }

    @Bean
    public Queue PushQueue0() {
        return new Queue(RabbitMqCon.PUSH_MATCHED_DATA_QUEUE0, true); // 第二个参数是durable，即是否持久化
    }
    @Bean
    public Queue PushQueue1() {
        return new Queue(RabbitMqCon.PUSH_MATCHED_DATA_QUEUE1, true); // 第二个参数是durable，即是否持久化
    }
    @Bean
    public Queue PushQueue2() {
        return new Queue(RabbitMqCon.PUSH_MATCHED_DATA_QUEUE2, true); // 第二个参数是durable，即是否持久化
    }
    @Bean
    public Queue PushQueue3() {
        return new Queue(RabbitMqCon.PUSH_MATCHED_DATA_QUEUE3, true); // 第二个参数是durable，即是否持久化
    }
    @Bean
    public Queue PushQueue4() {
        return new Queue(RabbitMqCon.PUSH_MATCHED_DATA_QUEUE4, true); // 第二个参数是durable，即是否持久化
    }

    // 绑定定向监测方案
    @Bean
    public Binding bindingDirection(Queue directionQueue, DirectExchange directExchange) {
        return BindingBuilder.bind(directionQueue).to(directExchange).with(RabbitMqCon.DIRECTION_KEY);

    }
    @Bean
    public Binding bindingDirectionTopic(Queue directionQueue, TopicExchange topicExchange) {
        return BindingBuilder.bind(directionQueue).to(topicExchange).with(RabbitMqCon.KEY);

    }

    // 绑定邯郸
    @Bean
    public Binding bindingHanDan(Queue hanDanQueue, DirectExchange directExchange) {
        return BindingBuilder.bind(hanDanQueue).to(directExchange).with(RabbitMqCon.HAN_DAN_KEY);

    }
    @Bean
    public Binding bindingHanDanTopic(Queue hanDanQueue, TopicExchange topicExchange) {
        return BindingBuilder.bind(hanDanQueue).to(topicExchange).with(RabbitMqCon.KEY);

    }
    // 绑定小极
    @Bean
    public Binding bindingXiaoJi(Queue xiaoJiQueue, DirectExchange directExchange) {
        return BindingBuilder.bind(xiaoJiQueue).to(directExchange).with(RabbitMqCon.XIAO_JI_KEY);
    }

    @Bean
    public Binding bindingXiaoJiTopic(Queue xiaoJiQueue, TopicExchange topicExchange) {
        return BindingBuilder.bind(xiaoJiQueue).to(topicExchange).with(RabbitMqCon.KEY);

    }
    // 绑定关键字
    @Bean
    public Binding bindingKeyword(Queue keywordQueue, DirectExchange directExchange) {
        return BindingBuilder.bind(keywordQueue).to(directExchange).with(RabbitMqCon.KEYWORD_KEY);
    }

    @Bean
    public Binding bindingKeywordTopic(Queue keywordQueue, TopicExchange topicExchange) {
        return BindingBuilder.bind(keywordQueue).to(topicExchange).with(RabbitMqCon.KEY);

    }

    // 绑定关键字表达式
    @Bean
    public Binding bindingKeywordExpression(Queue KeywordExpressionQueue, DirectExchange directExchange) {
        return BindingBuilder.bind(KeywordExpressionQueue).to(directExchange).with(RabbitMqCon.KEYWORD_EXPRESSION_KEY);
    }

    @Bean
    public Binding bindingKeywordExpressionTopic(Queue KeywordExpressionQueue, TopicExchange topicExchange) {
        return BindingBuilder.bind(KeywordExpressionQueue).to(topicExchange).with(RabbitMqCon.KEY);

    }

    // 推送的队列
    @Bean
    public Binding bindingPush(Queue PushQueue, DirectExchange directExchange) {
        return BindingBuilder.bind(PushQueue).to(directExchange).with(RabbitMqCon.PUSH_MATCHED_DATA_KEY);
    }
    @Bean
    public Binding bindingPush0(Queue PushQueue0, DirectExchange directExchange) {
        return BindingBuilder.bind(PushQueue0).to(directExchange).with(RabbitMqCon.PUSH_MATCHED_DATA_KEY0);
    }
    @Bean
    public Binding bindingPush1(Queue PushQueue1, DirectExchange directExchange) {
        return BindingBuilder.bind(PushQueue1).to(directExchange).with(RabbitMqCon.PUSH_MATCHED_DATA_KEY1);
    }
    @Bean
    public Binding bindingPush2(Queue PushQueue2, DirectExchange directExchange) {
        return BindingBuilder.bind(PushQueue2).to(directExchange).with(RabbitMqCon.PUSH_MATCHED_DATA_KEY2);
    }
    @Bean
    public Binding bindingPush3(Queue PushQueue3, DirectExchange directExchange) {
        return BindingBuilder.bind(PushQueue3).to(directExchange).with(RabbitMqCon.PUSH_MATCHED_DATA_KEY3);
    }
    @Bean
    public Binding bindingPush4(Queue PushQueue4, DirectExchange directExchange) {
        return BindingBuilder.bind(PushQueue4).to(directExchange).with(RabbitMqCon.PUSH_MATCHED_DATA_KEY4);
    }


    @Bean
    public Queue testQueue() {
        return new Queue(RabbitMqCon.KEYWORD_TEST_QUEUE, true); // 第二个参数是durable，即是否持久化
    }

    // 测试队列
    @Bean
    public Binding bindingTest(Queue testQueue, DirectExchange directExchange) {
        return BindingBuilder.bind(testQueue).to(directExchange).with(RabbitMqCon.KEYWORD_TEST_KEY);
    }

    @Bean
    public Queue testPushQueue() {
        return new Queue(RabbitMqCon.KEYWORD_TEST_PUSH_QUEUE, true); // 第二个参数是durable，即是否持久化
    }

    // 测试队列
    @Bean
    public Binding bindingPushTest(Queue testQueue, DirectExchange directExchange) {
        return BindingBuilder.bind(testQueue).to(directExchange).with(RabbitMqCon.KEYWORD_TEST_PSUH_KEY);
    }

//    // 测试队列
//    @Bean
//    public Binding bindingPushTestTopic(Queue testQueue, TopicExchange topicExchange) {
//        return BindingBuilder.bind(testQueue).to(topicExchange).with(RabbitMqCon.KEY);
//    }


    @Bean
    public TaskExecutor rabbitTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(40); // 初始线程数
        executor.setMaxPoolSize(60); // 最大线程数
        executor.setQueueCapacity(100); // 队列容量
        executor.setThreadNamePrefix("rabbitmq-thread-");
        executor.initialize();
        return executor;
    }
    @Bean
    public SimpleRabbitListenerContainerFactory factory1(ConnectionFactory connectionFactory, TaskExecutor rabbitTaskExecutor) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setTaskExecutor(rabbitTaskExecutor);
        factory.setConcurrentConsumers(20); // 初始并发消费
        factory.setMaxConcurrentConsumers(20); // 最大并发消费
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL); // 手动确认
        return factory;
    }


}
